package indi.gtl.superbrain.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import indi.gtl.superbrain.R;
import indi.gtl.superbrain.models.Constant;
import indi.gtl.superbrain.models.UserInfo;

import java.util.List;

/**
 * Created by admin1 on 15-5-27.
 */
public class ScanServicesAdapter extends BaseAdapter implements Constant {
    Context mContext;
    List<String[]> mServicesList;

    public ScanServicesAdapter(Context context, List<String[]> servicesList){
        mContext = context;
        mServicesList = servicesList;
    }

    public void updateList(List<String[]> servicesList) {
        mServicesList = servicesList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mServicesList.size();
    }

    @Override
    public Object getItem(int position) {
        return mServicesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.user_info_item_layout, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.userHeadItemImageView = (ImageView) convertView.findViewById(R.id.userHeadItemImageView);
            viewHolder.userNameItemTextView = (TextView) convertView.findViewById(R.id.userNameItemTextView);
            viewHolder.userScoreItemTextView = (TextView) convertView.findViewById(R.id.userScoreItemTextView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        UserInfo userInfo = UserInfo.parse(mServicesList.get(position)[1]);
        viewHolder.userHeadItemImageView.setImageResource(headList[userInfo.head % headList.length]);
        viewHolder.userNameItemTextView.setText(userInfo.name);
        viewHolder.userScoreItemTextView.setText(userInfo.sum_score + "");

        return convertView;
    }

    class ViewHolder{
        ImageView userHeadItemImageView;
        TextView userNameItemTextView;
        TextView userScoreItemTextView;
    }
}
